<?php

/* ----------------------------------------------
  Caloris: Administration Pages Editor

  $Id$
  $Author$

  Editor stránek s WYSIWYG editorem
  ---------------------------------------------- */

require_once("../../../index.php");
Caloris\ACL::authenticate();

define('_MODULE_ACTIVE', 'pageeditor');

if ($_POST["function_name"] == 'page_edit') {
    if (Caloris\ACL::authorize("templates") == 1) {
        $r = rawurldecode(stripslashes($_POST["content"]));
        $file = _CALSET_PATHS_BASE . _CALSET_DIRS_PAGES . '/' . $_POST["name"] . '.html';

        $fileHandle = fopen($file, 'w+', 1);
        fwrite($fileHandle, $r);
        fclose($fileHandle);


        if ($_POST["j"] != 1) {
            header("location: " . $_SERVER["HTTP_REFERER"] . "");
            exit();
        } else {
            echo "{\"msg\": \" uloženo (" . $_COOKIE["auser"] . ", " . date(_CALSET_BASIC_DATE_FORMAT . " " . _CALSET_BASIC_TIME_FORMAT) . ")\"}";
            exit();
        }
    } else {
        if ($_POST["j"] != 1) {
            header("location: " . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . "/pageeditor/pages.detail.php?name=" . $_POST["name"] . "&id=" . $_POST["name"] . "&msg=" . urlencode("Nemáte oprávnění"));
            exit();
        } else {
            echo "{\"msg\": \" Chyba, soubor nebyl uložen\"}";
        }

        exit();
    }
} elseif ($_POST["function_name"] == 'page_new') {
    if (Caloris\ACL::authorize("templates") == 1) {

        if ($_POST["name"] == 'administrator') {
            header("location: " . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . "/pageeditor/pages.php?msg=" . urlencode("Toto jméno je rezervováno a nelze použít"));
            exit();
        }

        // Založení html souboru v cal-pages 
        $fileHandle = fopen(_CALSET_PATHS_BASE . _CALSET_DIRS_PAGES . '/' . $_POST["name"] . '.html', 'w') or die("can't open file");
        fwrite($fileHandle, '');
        fclose($fileHandle);
        chmod(_CALSET_PATHS_BASE . _CALSET_DIRS_PAGES . '/' . $_POST["name"] . '.html', 0777);

        // Založení tagu v pages.xml
        $doc = new DOMDOcument;
        $doc->load(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/pages.xml');
        $xpath = new DOMXpath($doc);

        $qry = '/core';

        foreach ($xpath->query($qry) as $node) {
            $foo = $doc->createElement('page');
            $node->appendChild($foo);

            $idAttribute = $doc->createAttribute("id");
            $foo->appendChild($idAttribute);

            $priceValue = $doc->createTextNode($_POST["name"]);
            $idAttribute->appendChild($priceValue);

            $bar = $doc->createElement("enable", 0);
            $foo->appendChild($bar);
        }

        $doc->save(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/pages.xml');

        $tidy_options = array(
            'input-xml' => true,
            'output-xml' => true,
            'indent' => true,
            'wrap' => false,
        );

        $tidy = new tidy();
        $tidy->parseFile(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/pages.xml', $tidy_options, 'utf8');
        $tidy->cleanRepair();

        $xmm = simplexml_load_string($tidy);
        $xmm->asXML(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/pages.xml');

        header("location: " . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . "/pageeditor/pages.detail.php?name=" . $_POST["name"]);
        exit();
    } else {
        header("location: " . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . "/pageeditor/pages.php?msg=" . urlencode("Nemáte oprávnění"));
        exit();
    }
}

// Načtení informací z XML souboru

$content .= '
<h1>' . $_REQUEST["name"] . ' (<a href="pages.properties.php?name=' . $_GET["name"] . '">nastavení stránky</a>) <span id="msg" style="padding-left: 20px; color: #f77;"></span></h1>    

<form id="rpp" action="' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/pageeditor/pages.detail.php" method="post" onsubmit="return false;">';

if (file_exists(_CALSET_PATHS_BASE . _CALSET_DIRS_PAGES . '/' . $_REQUEST["name"] . '.html')) {
    $contentPage = file_get_contents(_CALSET_PATHS_BASE . _CALSET_DIRS_PAGES . '/' . $_REQUEST["name"] . '.html');
    
    if (_CALSET_BASIC_WYSIWYG == 1) {
        $setWysiwyg = ' id="wysiwyg"';
    }

    $content .= '
<input type="hidden" name="function_name" value="page_edit" /><input type="hidden" name="name" value="' . $_REQUEST["name"] . '" style="width: 100%;" />
<textarea name="content" ' . $setWysiwyg . ' style="width: 100%; height: 400px;">' . Caloris\Text::caltag($contentPage) . '</textarea>
<p class="perexu"><input type="submit" id="formxins" value="Uložit" class="btn btn-primary" /></p>
</form>
';
}

$data["title"] = 'Stránky: Editor';

$tmpl = new Caloris\Template;
$tmpl->input($content);
$tmpl->renderHead($data);
echo $tmpl->renderBody('caloris-admin');